package com.elookinto.uidhaverst;

import java.io.PrintStream;
import java.util.HashSet;
import java.util.Set;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;

public class Weuxuecity {

	public static void main(String s[]) throws Exception {

		loginPage();

	}
	
	//http://groups.wenxuecity.com/groupview.php?gid=1562#discussion
	//http://groups.wenxuecity.com/groupview.php?gid=1502
	//http://groups.wenxuecity.com/groupview.php?gid=1173

	public static void loginPage() throws Exception {
		PrintStream sysout = new PrintStream(System.out, true, "UTF-8");
		final WebClient webClient = new WebClient();
		Set<String> uids = new HashSet<String>();

		  HtmlPage page = webClient.getPage("http://bbs.wenxuecity.com");
		HtmlForm hf = (HtmlForm) page.getElementsByTagName("form").get(1);
		HtmlInput hi = hf.getInputByName("username");
		hi.setValueAttribute("el98012");
		  hi = hf.getInputByName("password");
		hi.setValueAttribute("liliping");
		  hi = hf.getInputByValue("登录");
		//hi.setValueAttribute("el98012");
		
		DomNodeList<HtmlElement> dl = hf.getElementsByTagName("input");
		for (int i=0; i<dl.getLength(); i++)
		{
			HtmlInput h = (HtmlInput)dl.get(i);
			sysout.println(h.getAttribute("type"));
		}
		
		//hi = hf.getInputByName("password");
		//hf.get
		//hi.setValueAttribute("liliping");
		page = (HtmlPage)hf.getInputByValue("登录").click();
		
		page = webClient.getPage("http://qqh.wenxuecity.com/compose.php");
		
		HtmlForm comf = page.getFormByName("mailbox");
		HtmlInput to = comf.getInputByName("to");
		HtmlInput text = comf.getInputByName("title");
		HtmlTextArea hat = comf.getTextAreaByName("msgbody");
		
		sysout.println(page.asXml());
		

	}

	public static void homePage() throws Exception {
		PrintStream sysout = new PrintStream(System.out, true, "UTF-8");
		final WebClient webClient = new WebClient();
		Set<String> uids = new HashSet<String>();
		for (int k = 0; k < 10; k++) {
			final HtmlPage page = webClient
					.getPage("http://bbs.wenxuecity.com/znjy/?page=" + k);

			Thread.currentThread().sleep(5000);
			DomNodeList<DomElement> dl = page.getElementsByTagName("a");
			for (int i = 0; i < dl.getLength(); i++) {
				HtmlAnchor ha = (HtmlAnchor) dl.get(i);
				String s = ha.getAttribute("class");
				if (s.equals("nickname")) {
					// sysout.println(ha.asText());
					uids.add(ha.asText());
				}

			}
			sysout.println(uids.size());
		}
		// Iterator<String> it = uids.iterator();

	}

}
